<?php

class channelComments
{
    private $m_db;

    public function __construct($strHost, $strUserName, $strPassWord, $strDatabase)
    {
        $this->m_db = new mysqli($strHost, $strUserName, $strPassWord, $strDatabase);
        if (mysqli_connect_errno())
        {
            throw 'Error: Could not connect to database. Please try again later.';
        }
    }

    public function __destruct()
    {
        $this->m_db->close();
    }

    public function commentsList($intUserID, $intPageNo, $intListNum)
    {
        $intBeginNo = $intPageNo * $intListNum;
        $FMT_COMMENTS_LIST_SQL = 'SELECT comment_id, from_user_id, from_ip, '.
            'creat_time, user_url, from_user_name, comment_content '.
            'FROM comments WHERE user_id = %u LIMIT %u, %u';
        $strSQL = sprintf($FMT_COMMENTS_LIST_SQL, $intUserID, $intBeginNo, $intListNum);
        return $this->getList($strSQL);
    }

    public function addComment($arrCmtInfo)
    {
        if (   !array_key_exists("intUid",        $arrCmtInfo)
            || !array_key_exists("intFromUid",    $arrCmtInfo)
            || !array_key_exists("intFromIp",     $arrCmtInfo)
            || !array_key_exists("strFromUidURL", $arrCmtInfo)
            || !array_key_exists("strFromUname",  $arrCmtInfo)
            || !array_key_exists("strCmtContent", $arrCmtInfo)
           )
        {
            return false;
        }
        echo "pass\n";
        $intTime = time();
        // 向comment_id插入一条数据
        $FMT_INSERT_ID_SQL = 'INSERT INTO comment_id (user_id) VALUES(%u)';
        $strSQL = sprintf($FMT_INSERT_ID_SQL, $arrCmtInfo['intUid']);
        $this->Update($strSQL);
        $arrCmtInfo['intCommentID'] = mysqli_insert_id($this->m_db);
        assert(0 < $arrCmtInfo['intCommentID']);
        // 向comments插入一条数据
        $FMT_INSERT_COMMENT_SQL = 'INSERT INTO comments (user_id, comment_id, '.
            'from_user_id, from_ip, creat_time, show_able, user_url, from_user_name, comment_content) '.
            'VALUES (%u, %u, %u, %u, %u, %u, "%s", "%s", "%s");';
        $strSQL = sprintf($FMT_INSERT_COMMENT_SQL, $arrCmtInfo['intUid'], $arrCmtInfo['intCommentID'],
            $arrCmtInfo['intFromUid'], $arrCmtInfo['intFromIp'], $intTime, 1,
            $arrCmtInfo['strFromUidURL'], $arrCmtInfo["strFromUname"], $arrCmtInfo["strCmtContent"]);
        $this->Update($strSQL);
    }

    public function delComment($intUserId, $intCommentID)
    {
        $FMT_DELETE_COMMENT_SQL = 'DELETE FROM comments WHERE user_id = %u AND comment_id = %u';
        $strSQL = sprintf($FMT_DELETE_COMMENT_SQL, $intUserId, $intCommentID);
        $this->Update($strSQL);
    }

    private function Update($strSQL)
    {
//        $strSQL = $this->m_db->escape_string($strSQL);
        assert ( true == $this->m_db->real_query($strSQL));
//        echo $strSQL."##".mysqli_error($this->m_db)."\n";
    }

    private function getSingleRow($strSQL)
    {
//        echo $strSQL.'\n';
        $result_row = false;
        if (false === $this->m_db->real_query($this->m_db->escape_string($strSQL)))
        {
//            echo mysqli_error($this->m_db).'\n';
            return false;
        }
        else
        {
            if ($result = $this->m_db->use_result())
            {
                $result_row = $result->fetch_assoc();
                $result->close();
            }
        }
        return $result_row;
    }

    private function getList($strSQL)
    {
//        echo $strSQL.'\n';
        $result_list = array();
        if (false === $this->m_db->real_query($this->m_db->escape_string($strSQL)))
        {
//            echo mysqli_error($this->m_db).'\n';
            return false;
        }
        else
        {
            if ($result = $this->m_db->use_result()) {
                while ($row = $result->fetch_assoc()) {
                    $result_list[] = $row;
                }
                $result->close();
            }
        }
        return $result_list;
    }

};
?>
